<?php
namespace App\Controller;
use Think\Controller;
class ApiController extends Controller
{
    public function __construct()
    {
        //先调用父类构造函数
        parent::__construct();
        //判断登录
        if(IS_POST){
            $this->check_key(I('post.key'));
        }
    }
    public function check_key($key){
           if($key!=C('key')){ json_Response(-200,'没有权限',null);}
       }





    //响应前台的请求
    public function respond(){
        //验证身份
        $timeStamp = I('post.t');
        $randomStr = I('post.r');
        $signature = I('post.s');
        $str = $this -> arithmetic($timeStamp,$randomStr);
        if($str != $signature){
            echo "-1";
            exit;
        }
    }
    /**
     * @param $timeStamp 时间戳
     * @param $randomStr 随机字符串
     * @return string 返回签名
     */
    public function arithmetic($timeStamp,$randomStr){
        $arr['timeStamp'] = $timeStamp;
        $arr['randomStr'] = $randomStr;
        $arr['token'] = C('key');
        //按照首字母大小写顺序排序
        sort($arr,SORT_STRING);
        //拼接成字符串
        $str = implode($arr);
        //进行加密
        $signature = sha1($str);
        $signature = md5($signature);
        //转换成大写
        $signature = strtoupper($signature);
        return $signature;
    }
}
